/**
 * @author Wang Hai Long  [whailong2010@gmail.com]
 * @date Oct 30, 2014 1:57:28 PM
 */
package com.surent.controller.activity;

import java.text.Collator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.hailong.surent.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.surent.utils.OnclikViewRight;
import com.surent.utils.PingYinUtil;
import com.surent.utils.TextBaseAdapter;
import com.surent.view.custom.SeekBar_Right;

/**
 * @author Wang Hai Long [whailong2010@gmail.com]
 * @date Oct 30, 2014 1:57:28 PM
 */
@ContentView(R.layout.aty_indexlistview)
public class AtyIndexListview extends SwipeBackActivity {

	/** 列表视图 */
	private ListView listView;

	/** 列表内容 */
	private String[] strData = { "七里香", "东风破", "兰亭序", "半兽人", "搁浅", "以父之名", "你听的到", "回到过去", "一路向北", "枫", "夜曲", "可爱女人",
			"最后的战役", "爱在西元前", "开不了口", "半岛铁盒", "借口", "轨迹", "烟花易冷", "双截棍", "忍者", "晴天", "威廉古保", "星晴", "黑色幽默", "菊花台",
			"说了再见", "退后", "甜甜的", "最长的电影", "止战之伤", "世界末日" };

	/** 列表字母标题 */
	private char[] chaTitle = new char[strData.length];

	/** 自定义View */
	private SeekBar_Right seekbar_Right;

	/** 适配器 */
	private TextBaseAdapter textBaseAdapter;

	/** 标签提示 */
	private TextView text_view;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
		dataInit();
		viewInit();
	}

	public void dataInit() {
		chinaSort();
		textBaseAdapter = new TextBaseAdapter(this, strData, chaTitle);
	}

	public void viewInit() {
		listView = (ListView) findViewById(R.id.listView);
		listView.setAdapter(textBaseAdapter);
		seekbar_Right = (SeekBar_Right) findViewById(R.id.view_right);
		seekbar_Right.setOnclikViewRight(onclikViewRight);
		text_view = (TextView) findViewById(R.id.text_tag);
	}

	/**
	 * 中文排序
	 */
	public void chinaSort() {
		Comparator<Object> cmp = Collator.getInstance(Locale.CHINA);
		Arrays.sort(strData, cmp);
		for (int i = 0; i < strData.length; i++) {
			chaTitle[i] = PingYinUtil.converterToFirstSpell(strData[i]).charAt(0);
		}
	}

	/**
	 * 实现索引View的监听接口
	 */
	private OnclikViewRight onclikViewRight = new OnclikViewRight() {
		@Override
		public void seteventUP() {
			text_view.setVisibility(View.GONE);
		}

		@Override
		public void seteventDownAndMove(String str) {
			text_view.setText(str);
			text_view.setVisibility(View.VISIBLE);

			for (int i = 0; i < chaTitle.length; i++) {

				if (str.charAt(0) == chaTitle[i]) {

					listView.setSelection(i); // LIST相应移动
					break;
				}
			}
		}
	};
}
